import { prisma } from '../lib/database.js'

async function seedCategories() {
  console.log('🌱 Seeding categories...')

  try {
    // 清理现有分类
    await prisma.category.deleteMany()

    // 创建分类
    const categories = await prisma.category.createMany({
      data: [
        {
          name: 'productivity',
          displayName: 'Productivity',
          description: 'Tools to boost your productivity and efficiency',
          icon: 'productivity',
          color: '#3B82F6',
          sortOrder: 1
        },
        {
          name: 'development',
          displayName: 'Development',
          description: 'Development tools and utilities for coding',
          icon: 'code',
          color: '#10B981',
          sortOrder: 2
        },
        {
          name: 'ui-ux',
          displayName: 'UI/UX',
          description: 'User interface and experience design tools',
          icon: 'design',
          color: '#F59E0B',
          sortOrder: 3
        },
        {
          name: 'automation',
          displayName: 'Automation',
          description: 'Workflow automation and task scheduling tools',
          icon: 'automation',
          color: '#8B5CF6',
          sortOrder: 4
        },
        {
          name: 'utilities',
          displayName: 'Utilities',
          description: 'General purpose utilities and helper tools',
          icon: 'utilities',
          color: '#6B7280',
          sortOrder: 5
        },
        {
          name: 'communication',
          displayName: 'Communication',
          description: 'Communication and collaboration tools',
          icon: 'communication',
          color: '#EF4444',
          sortOrder: 6
        },
        {
          name: 'media',
          displayName: 'Media',
          description: 'Media processing and manipulation tools',
          icon: 'media',
          color: '#F97316',
          sortOrder: 7
        },
        {
          name: 'security',
          displayName: 'Security',
          description: 'Security and privacy protection tools',
          icon: 'security',
          color: '#DC2626',
          sortOrder: 8
        }
      ]
    })

    console.log(`✅ Created ${categories.count} categories`)

    // 创建一些标签
    await prisma.tag.deleteMany()
    
    const tags = await prisma.tag.createMany({
      data: [
        {
          name: 'popular',
          displayName: 'Popular',
          description: 'Popular plugins with high download counts',
          color: '#EF4444'
        },
        {
          name: 'featured',
          displayName: 'Featured',
          description: 'Featured plugins recommended by the platform',
          color: '#F59E0B'
        },
        {
          name: 'new',
          displayName: 'New',
          description: 'Recently added plugins',
          color: '#10B981'
        },
        {
          name: 'recommended',
          displayName: 'Recommended',
          description: 'Recommended by the community',
          color: '#3B82F6'
        },
        {
          name: 'editor',
          displayName: 'Editor',
          description: 'Text editor and IDE related plugins',
          color: '#8B5CF6'
        },
        {
          name: 'workflow',
          displayName: 'Workflow',
          description: 'Workflow optimization tools',
          color: '#06B6D4'
        },
        {
          name: 'api',
          displayName: 'API',
          description: 'API development and testing tools',
          color: '#84CC16'
        },
        {
          name: 'database',
          displayName: 'Database',
          description: 'Database management and tools',
          color: '#A855F7'
        }
      ]
    })

    console.log(`✅ Created ${tags.count} tags`)

    // 创建一个测试开发者
    const existingDev = await prisma.developer.findUnique({
      where: { email: 'test@example.com' }
    })

    if (!existingDev) {
      const testDev = await prisma.developer.create({
        data: {
          username: 'test_dev',
          email: 'test@example.com',
          passwordHash: '$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewdBPj/RK.s5uO.G', // password123
          displayName: 'Test Developer',
          bio: 'A test developer account for development',
          verified: true,
          emailVerified: true,
          status: 'active'
        }
      })

      console.log(`✅ Created test developer: ${testDev.username}`)
    } else {
      console.log(`✅ Test developer already exists: ${existingDev.username}`)
    }

    console.log('✅ Categories and tags seeding completed!')

  } catch (error) {
    console.error('❌ Error seeding categories:', error)
    throw error
  }
}

seedCategories()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })
